clear
set more off
*cd "C:/Users/peterel/Dropbox/Class Panel Study (BHPS)/BHPS Files_tom"
cd ""

*******************************************************************************************************************************************

** SECTION 1: CREATING DATASET


* Create Datasets for each wave with standardised names
	use aindresp.dta,clear
	keep pid avote aage aopsoca aopsocb aopsocc aopsocd aopsoce aopsocf ajbgold apagold amagold asex aopcls1 aopcls2 aopcls3 aspsoc aopfamf afiyr aisced
	rename avote partyid
	rename aage age
	rename aopsoca sharewealth
	rename aopsocb onelaw
	rename aopsocc privent
	rename aopsocd pubservs
	rename aopsoce govjobs
	rename aopsocf unions
	rename ajbgold jbgold
	rename apagold pagold
	rename amagold magold
	rename asex sex
	rename aopcls1 belongclass
	rename aopcls2 subjclass
	rename aopcls3 oppclass
	rename aspsoc spsoc
	rename aopfamf opfamf
	rename afiyr fiyr
	rename aisced isced
	gen wave =1
	save wavemerge1.dta, replace

	use bindresp.dta,clear
	keep pid bage boppolc bsampst bjbgold bsex bspsoc bfiyr bisced
	rename bage age
	rename boppolc maxinc
	rename bsampst sampst
	rename bjbgold jbgold
	rename bsex sex
	rename bspsoc spsoc
	rename bfiyr fiyr
	rename bisced isced
	gen wave=2
	save wavemerge2.dta, replace

	use cindresp.dta,clear
	keep pid cvote cage copsoca copsocb copsocc copsocd copsoce copsocf csampst cjbgold csex cspsoc copfamf cfiyr cisced
	rename cvote partyid
	rename cage age
	rename copsoca sharewealth
	rename copsocb onelaw
	rename copsocc privent
	rename copsocd pubservs
	rename copsoce govjobs
	rename copsocf unions
	rename csampst sampst
	rename cjbgold jbgold
	rename csex sex
	rename cspsoc spsoc
	rename copfamf opfamf
	rename cfiyr fiyr
	rename cisced isced
	gen wave =3
	save wavemerge3.dta, replace

	use dindresp.dta,clear
	keep pid dage doppolc dvote dsampst djbgold dsex dspsoc dfiyr disced
	rename dvote partyid
	rename dage age
	rename doppolc maxinc
	rename dsampst sampst
	rename djbgold jbgold
	rename dsex sex
	rename dspsoc spsoc
	rename dfiyr fiyr
	rename disced isced
	gen wave=4
	save wavemerge4.dta, replace

	use eindresp.dta,clear
	keep pid evote eage eopsoca eopsocb eopsocc eopsocd eopsoce eopsocf esampst ejbgold esex espsoc eopfamf efiyr eisced
	rename evote partyid
	rename eage age
	rename eopsoca sharewealth
	rename eopsocb onelaw
	rename eopsocc privent
	rename eopsocd pubservs
	rename eopsoce govjobs
	rename eopsocf unions
	rename esampst sampst
	rename ejbgold jbgold
	rename esex sex
	rename espsoc spsoc
	rename eopfamf opfamf
	rename efiyr fiyr
	rename eisced isced
	gen wave =5
	save wavemerge5.dta, replace

	use findresp.dta,clear
	keep pid fage foppolc fvote fsampst fjbgold fsex fopcls1 fopcls2 fopcls3 fspsoc ffiyr fisced
	rename fvote partyid
	rename fage age
	rename foppolc maxinc
	rename fsampst sampst
	rename fjbgold jbgold
	rename fsex sex
	rename fopcls1 belongclass
	rename fopcls2 subjclass
	rename fopcls3 oppclass
	rename fspsoc spsoc
	rename ffiyr fiyr
	rename fisced isced
	gen wave=6
	save wavemerge6.dta, replace

	use gindresp.dta,clear
	keep pid gvote gage gopsoca gopsocb gopsocc gopsocd gopsoce gopsocf gsampst gjbgold gsex gspsoc gopfamf gfiyr gisced
	rename gvote partyid
	rename gage age
	rename gopsoca sharewealth
	rename gopsocb onelaw
	rename gopsocc privent
	rename gopsocd pubservs
	rename gopsoce govjobs
	rename gopsocf unions
	rename gsampst sampst
	rename gjbgold jbgold
	rename gsex sex
	rename gspsoc spsoc
	rename gopfamf opfamf
	rename gfiyr fiyr
	rename gisced isced
	gen wave =7
	save wavemerge7.dta, replace

	use hindresp.dta,clear
	keep pid hage hoppolc hvote hsampst hjbgold hpagold hmagold hj1gold hsex hopfamr hspsoc hfiyr hisced
	rename hvote partyid
	rename hage age
	rename hoppolc maxinc
	rename hsampst sampst
	rename hjbgold jbgold
	rename hpagold pagold
	rename hmagold magold
	rename hj1gold j1gold
	rename hsex sex
	rename hopfamr homosex
	rename hspsoc spsoc
	rename hfiyr fiyr
	rename hisced isced
	gen wave=8
	save wavemerge8.dta, replace

	use iindresp.dta,clear
	keep pid ivote iage isampst ijbgold ipagold imagold ij1gold isex ispsoc ifiyr iisced
	rename ivote partyid
	rename iage age
	rename isampst sampst
	rename ijbgold jbgold
	rename ipagold pagold
	rename imagold magold
	rename ij1gold j1gold
	rename isex sex
	rename ispsoc spsoc
	rename ifiyr fiyr
	rename iisced isced
	gen wave =9
	save wavemerge9.dta, replace

	use jindresp.dta,clear
	keep pid jage  jvote jopsoca jopsocb jopsocc jopsocd jopsoce jopsocf jsampst jjbgold jpagold jmagold jj1gold jsex jopcls1 jopcls2 jopcls3 jopfamr jspsoc jfiyr jisced
	rename jvote partyid
	rename jage age
	rename jopsoca sharewealth
	rename jopsocb onelaw
	rename jopsocc privent
	rename jopsocd pubservs
	rename jopsoce govjobs
	rename jopsocf unions
	rename jsampst sampst
	rename jjbgold jbgold
	rename jpagold pagold
	rename jmagold magold
	rename jj1gold j1gold
	rename jsex sex
	rename jopcls1 belongclass
	rename jopcls2 subjclass
	rename jopcls3 oppclass
	rename jopfamr homosex
	rename jspsoc spsoc
	rename jfiyr fiyr
	rename jisced isced
	gen wave=10
	save wavemerge10.dta, replace

	use kindresp.dta,clear
	keep pid kvote kage koppolc ksampst kjbgold kpagold kmagold kj1gold ksex kspsoc kopfamf kfiyr kisced
	rename kvote partyid
	rename kage age
	rename koppolc maxinc
	rename ksampst sampst
	rename kjbgold jbgold
	rename kpagold pagold
	rename kmagold magold
	rename kj1gold j1gold
	rename ksex sex
	rename kspsoc spsoc
	rename kopfamf opfamf
	rename kfiyr fiyr
	rename kisced isced
	gen wave =11
	save wavemerge11.dta, replace

	use lindresp.dta,clear
	keep pid lage lvote lsampst ljbgold lpagold lmagold lj1gold lsex lopfamr lspsoc lfiyr lisced
	rename lvote partyid
	rename lage age
	rename lsampst sampst
	rename ljbgold jbgold
	rename lpagold pagold
	rename lmagold magold
	rename lj1gold j1gold
	rename lsex sex
	rename lopfamr homosex
	rename lspsoc spsoc
	rename lfiyr fiyr
	rename lisced isced
	gen wave=12
	save wavemerge12.dta, replace

	use mindresp.dta,clear
	keep pid mvote mage moppolc msampst mjbgold mpagold mmagold mj1gold msex mspsoc mopfamf mfiyr misced
	rename mvote partyid
	rename mage age
	rename moppolc maxinc
	rename msampst sampst
	rename mjbgold jbgold
	rename mpagold pagold
	rename mmagold magold
	rename mj1gold j1gold
	rename msex sex
	rename mspsoc spsoc
	rename mopfamf opfamf
	rename mfiyr fiyr
	rename misced isced
	gen wave =13
	save wavemerge13.dta, replace

	use nindresp.dta,clear
	keep pid nage nvote nopsoca nopsocb nopsocc nopsocd nopsoce nopsocf nsampst njbgold npagold nmagold nj1gold nsex nopfamr nspsoc nfiyr nisced
	rename nvote partyid
	rename nage age
	rename nopsoca sharewealth
	rename nopsocb onelaw
	rename nopsocc privent
	rename nopsocd pubservs
	rename nopsoce govjobs
	rename nopsocf unions
	rename nsampst sampst
	rename njbgold jbgold
	rename npagold pagold
	rename nmagold magold
	rename nj1gold j1gold
	rename nsex sex
	rename nopfamr homosex
	rename nspsoc spsoc
	rename nfiyr fiyr
	rename nisced isced
	gen wave=14
	save wavemerge14.dta, replace

	use oindresp.dta,clear
	keep pid oage ovote osampst ojbgold opagold omagold oj1gold osex oopcls1 oopcls2 oopcls3 ospsoc oopfamf ofiyr oisced
	rename ovote partyid
	rename oage age
	rename osampst sampst
	rename ojbgold jbgold
	rename opagold pagold
	rename omagold magold
	rename oj1gold j1gold
	rename osex sex
	rename oopcls1 belongclass
	rename oopcls2 subjclass
	rename oopcls3 oppclass
	rename ospsoc spsoc
	rename oopfamf opfamf
	rename ofiyr fiyr
	rename oisced isced
	gen wave=15
	save wavemerge15.dta, replace

	use pindresp.dta,clear
	keep pid pvote page poppolc psampst pjbgold ppagold pmagold pj1gold psex pspsoc pfiyr pisced
	rename pvote partyid
	rename page age
	rename poppolc maxinc
	rename psampst sampst
	rename pjbgold jbgold
	rename ppagold pagold
	rename pmagold magold
	rename pj1gold j1gold
	rename psex sex
	rename pspsoc spsoc
	rename pfiyr fiyr
	rename pisced isced
	gen wave =16
	save wavemerge16.dta, replace

	use qindresp.dta,clear
	keep pid qage qvote qopsoca qopsocb qopsocc qopsocd qopsoce qopsocf qsampst qjbgold qpagold qmagold qj1gold qsex qspsoc qopfamf qfiyr qisced
	rename qvote partyid
	rename qage age
	rename qopsoca sharewealth
	rename qopsocb onelaw
	rename qopsocc privent
	rename qopsocd pubservs
	rename qopsoce govjobs
	rename qopsocf unions
	rename qsampst sampst
	rename qjbgold jbgold
	rename qpagold pagold
	rename qmagold magold
	rename qj1gold j1gold
	rename qsex sex
	rename qspsoc spsoc
	rename qopfamf opfamf
	rename qfiyr fiyr
	rename qisced isced
	gen wave=17
	save wavemerge17.dta, replace

	use rindresp.dta,clear
	keep pid rage rvote rsampst rjbgold rpagold rmagold rj1gold rsex ropfamr rspsoc rfiyr risced
	rename rvote partyid
	rename rage age
	rename rsampst sampst
	rename rjbgold jbgold
	rename rpagold pagold
	rename rmagold magold
	rename rj1gold j1gold
	rename rsex sex
	rename ropfamr homosex
	rename rspsoc spsoc
	rename rfiyr fiyr
	rename risced isced
	gen wave=18
	save wavemerge18.dta, replace

* Merge together waves
	use wavemerge1.dta
	foreach y in 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17{
	append using wavemerge`y'.dta
	}

**************************************************************************************

** CODING OF VARIABLES **

* 5-category class scheme
	gen egp =.
	replace egp = 5 if jbgold==1 // higher service
	replace egp = 4 if jbgold==2 //lower service
	replace egp = 3 if jbgold==3  // Routine non-manual 
	replace egp = 2 if jbgold==8 | jbgold==9 // foreman+technicians, skilled manual
	replace egp = 1 if jbgold==10 | jbgold==11 | jbgold==4 // non- or semi-skilled manual, agricultural, personal service

* 4-category class scheme
	gen egp4 = .
	replace egp4 = 4 if egp==5
	replace egp4 = 3 if egp==4
	replace egp4 = 2 if egp==3 
	replace egp4 = 1 if egp==1 | egp==2 
	label define egp4 1 "Workers" 2 "Routine non-manual" 3 "Lower service" 4 "Higher service"
	label values egp4 egp4 

* Create class dummies [routine non-manual = omitted category]
	gen worker = .
	replace worker = 1 if egp==1|egp==2
	replace worker = 0 if egp==3|egp==4|egp==5
		
	gen lowservice = .
	replace lowservice = 1 if egp==4
	replace lowservice = 0 if egp==1|egp==2|egp==3|egp==5
		
	gen highservice = .
	replace highservice = 1 if egp==5
	replace highservice = 0 if egp==1|egp==2|egp==3|egp==4
	
	gen routine = .
	replace routine = 1 if egp==3
	replace routine = 0 if egp==1|egp==2|egp==4|egp==5

* Creating ideology scale: originally coded so that higher values are more conservative
	recode onelaw (-9 = .) (-7 =.) (-2=.) (-1=.)
	recode pubservs (-9 = .) (-7 =.) (-2=.) (-1=.)
	recode govjobs (-9 = .) (-7 =.) (-2=.) (-1=.)
	recode unions (-9 = .) (-7 =.) (-2=.) (-1=.)

	gen privent_rev = .
	replace privent_rev = 1 if privent==5
	replace privent_rev = 2 if privent==4
	replace privent_rev = 3 if privent==3
	replace privent_rev = 4 if privent==2
	replace privent_rev = 5 if privent==1

	gen sharewealth_rev = .
	replace sharewealth_rev = 1 if sharewealth==5
	replace sharewealth_rev = 2 if sharewealth==4
	replace sharewealth_rev = 3 if sharewealth==3
	replace sharewealth_rev = 4 if sharewealth==2
	replace sharewealth_rev = 5 if sharewealth==1


	gen econ_ideol = privent_rev + sharewealth_rev + onelaw + pubservs + govjobs + unions if (sharewealth_rev!=. & privent_rev!=. & onelaw!=. & pubservs!=. & govjobs!=. & unions!=.)
	gen eclr = (econ_ideol/6)  /* still higher = more cons, now on a 1-5 scale*/

* Homosexuality, again higher = more conservative
	gen homowrong = .
	replace homowrong=1 if homosex==5
	replace homowrong=2 if homosex==4
	replace homowrong=3 if homosex==3
	replace homowrong=4 if homosex==2
	replace homowrong=5 if homosex==1
	label define homowrong 1 "Strongly disagree" 2 "Disagree" 3 "Neither" 4 "agree" 5 "Strongly agree"
	label values homowrong homowrong
	
* Gender equality 
	gen wifehome = . 
	replace wifehome = 1 if opfamf==5
	replace wifehome = 2 if opfamf==4
	replace wifehome = 3 if opfamf==3
	replace wifehome = 4 if opfamf==2
	replace wifehome = 5 if opfamf==1
	label define wifehome 1 "Strongly disagree" 2 "Disagree" 3 "Neither" 4 "agree" 5 "Strongly agree"
	label values wifehome wifehome 
	tab wifehome opfamf
		
* Subjective Class
	gen subjclass7 = . 
	replace subjclass7 = subjclass if subjclass>0 & subjclass<8 
	tab subjclass7 subjclass

	gen subjclass2 = .
	replace subjclass2 = 0 if subjclass7<4
	replace subjclass2 = 1 if subjclass7>3 & subjclass7!=.
	label define subjclass2 0 "Working class" 1 "Middle class"
	label values subjclass2 subjclass2

* Voting Conservative
	gen cons=.
	replace cons = 1 if partyid==1
	replace cons = 0 if partyid!=1 & partyid>0 & partyid<7 
	
* Creating education variable
	gen edu3 = 1 if isced==1 | isced==2 // primary and low secondary
	replace edu3 = 2 if isced==3 | isced==4 | isced==5 // low sec-voc, hisec-mivoc, higher voc
	replace edu3 = 3 if isced==4 | isced==5 // Higher education
	label define edu3 1 "No/low edu." 2 "Medium edu." 3 "Higher edu."
	label values edu3 edu3

* Creating income variable (annual income in 1000 pounds)
	gen fiyr1000 = fiyr/1000 if fiyr>-1


**************************************************************************************

** FINAL DATASET **

* Identify individuals with no employment info, mostly retired
	bys pid: egen numegp=sum(egp)
	drop if numegp==0
	drop numegp
	drop if worker==.| lowservice ==. | highservice ==.
	
* Create panel
	tsset pid wave
	bys pid: gen nobs=[_N] 

* order the variables
	order eclr econ_ideol homowrong wifehome cons sharewealth onelaw privent pubservs govjobs unions maxinc homosex partyid fiyr, first
	order jbgold subjclass2 egp4 egp j1gold belongclass subjclass oppclass spsoc isced, first
	order pid wave sampst nobs sex age fiyr1000 edu3, first
	
* drop people in for only one wave 
	drop if nobs==1
	
****************************************************************************************
****************************************************************************************

** SECTION 2: ANALYSIS **


* 1. Transition Probabilities and Summaries 
* --------------------------------------------------------------------
	preserve
	keep if wave==1|wave==3|wave==5|wave==7|wave==10|wave==14|wave==17 /*restrict to waves where we observe economic ideology*/
	keep if eclr!=.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	codebook pid

	* appendix cross tabs
	xttab egp4  
	xttrans egp4

		
* 2. Main Regressions
* ------------------------------------------------------ 
	
* Economic ideology
	xtreg eclr worker lowservice highservice i.wave
	xtreg eclr worker lowservice highservice i.wave, fe cl(pid)
	restore	

* Homosexuality 
	preserve
	keep if wave==8|wave==10|wave==12|wave==14
	drop if homowrong==.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
		
	xtreg homowrong worker lowservice highservice i.wave, be
	xtreg homowrong worker lowservice highservice i.wave, fe cl(pid)
	restore
	
* Gender equality 
	preserve
	keep if wave==1|wave==3|wave==5|wave==7|wave==11|wave==13|wave==15|wave==17	
	drop if wifehome==.	
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	xtreg wifehome worker lowservice highservice i.wave, be
	xtreg wifehome worker lowservice highservice i.wave, fe cl(pid)
	restore

* Subjective Class 
	preserve
	keep if wave==1|wave==6|wave==10|wave==15
	drop if subjclass2==.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	xtreg subjclass2 worker lowservice highservice i.wave, be 
	xtreg subjclass2 worker lowservice highservice i.wave, fe cl(pid)
	
	*logit subjclass2 worker lowservice highservice i.wave
	restore
	
* Voting 
	xtreg cons worker lowservice highservice i.wave, be
	xtreg cons worker lowservice highservice i.wave, fe cl(pid)
	
	*logit cons worker lowservice highservice i.wave

* 3. Robustness 
* --------------------------------------------------------------------------- 

* Income and Education

	* Income ---> eclr (Table 4)
	preserve
	keep if wave==1|wave==3|wave==5|wave==7|wave==10|wave==14|wave==17 /*restrict to waves where we observe economic ideology*/
	drop if eclr==.
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	xtreg eclr fiyr1000 i.wave, be
	xtreg eclr fiyr1000 i.wave, fe cl(pid)

	
	* Economic ideology (appendix)
	xtreg eclr worker lowservice highservice i.wave if edu3!=. & fiyr1000!=., fe cl(pid)
	xtreg eclr worker lowservice highservice i.edu3 fiyr1000 i.wave, fe cl(pid)
	restore
	
	* Homosexuality (appendix)
	preserve
	keep if wave==8|wave==10|wave==12|wave==14
	drop if homowrong==.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
				
	xtreg homowrong worker lowservice highservice i.wave if edu3!=. & fiyr1000!=., fe cl(pid)
	xtreg homowrong worker lowservice highservice i.edu3 fiyr1000 i.wave, fe cl(pid)
	restore
	
	*Gender equality (appendix)
	preserve
	keep if wave==1|wave==3|wave==5|wave==7|wave==11|wave==13|wave==15|wave==17	
	drop if wifehome==.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	xtreg wifehome worker lowservice highservice i.wave if edu3!=. & fiyr1000!=., fe cl(pid)
	xtreg wifehome worker lowservice highservice i.edu3 fiyr1000 i.wave, fe cl(pid)
	restore
	
	*Subjective class (appendix)
	preserve
	keep if wave==1|wave==6|wave==10|wave==15
	drop if subjclass2==.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	xtreg subjclass2 worker lowservice highservice i.wave if edu3!=. & fiyr1000!=., fe cl(pid)
	xtreg subjclass2 worker lowservice highservice i.edu3 fiyr1000 i.wave, fe cl(pid)
	restore
	
	*Voting (appendix)
	xtreg cons worker lowservice highservice i.wave if edu3!=. & fiyr1000!=., fe cl(pid)
	xtreg cons worker lowservice highservice i.edu3 fiyr1000 i.wave, fe cl(pid)

	* Class ---> Income 
	preserve
	keep if wave==1|wave==3|wave==5|wave==7|wave==10|wave==14|wave==17 /*restrict to waves where we observe economic ideology*/
	drop if eclr==.
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	xtreg fiyr1000 worker lowservice highservice i.wave, be
	xtreg fiyr1000 worker lowservice highservice i.wave, fe cl(pid)

	summ fiyr1000
	restore 
	
	
* Parallel trends (fig): variables created here, exported to R for plotting [same dataset also used for appendix fig on changes]

	preserve
	keep if wave==1|wave==3|wave==5|wave==7|wave==10|wave==14|wave==17 /*restrict to waves where we observe ideology*/
	keep if eclr!=.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	gen class_ch=.
	by pid: replace class_ch = egp4 - egp4[_n-1] 

	gen wavegap =.
	by pid: replace wavegap = wave - wave[_n-1]

	
	* follow individuals: waves 1-3-5, change between 3 and 5
	gen sustup_135=.
	by pid: replace sustup_135 = 1 if class_ch==1 & egp4==3 & class_ch[_n-1]!=. & class_ch!=. & wavegap==2 & wavegap[_n-1]==2 & wave==5
	by pid: replace sustup_135 = 1 if sustup_135[_n+1] ==1 & wave==3
	by pid: replace sustup_135 = 1 if sustup_135[_n+1] ==1 & wave==1

	gen sustup2_135=.
	by pid: replace sustup2_135 = 1 if class_ch==2 & egp4==4 & class_ch[_n-1]!=. & class_ch!=. & wavegap==2 & wavegap[_n-1]==2 & wave==5
	by pid: replace sustup2_135 = 1 if sustup2_135[_n+1] ==1 & wave==3
	by pid: replace sustup2_135 = 1 if sustup2_135[_n+1] ==1 & wave==1
	
	gen sustnoch_135=.
	by pid: replace sustnoch_135 = 1 if class_ch==0 & egp4==2 & class_ch[_n-1]!=. & class_ch!=. & wavegap==2 & wavegap[_n-1]==2 & wave==5
	by pid: replace sustnoch_135 = 1 if sustnoch_135[_n+1] ==1 & wave==3
	by pid: replace sustnoch_135 = 1 if sustnoch_135[_n+1] ==1 & wave==1

	
	* follow individuals: waves 3-5-7, change between 3 and 5
	gen sustup_357=.
	by pid: replace sustup_357 = 1 if class_ch==1 & egp4==3 & class_ch[_n-1]!=. & class_ch!=. & wavegap==2 & wavegap[_n-1]==2 & wave==7
	by pid: replace sustup_357 = 1 if sustup_357[_n+1] ==1 & wave==5
	by pid: replace sustup_357 = 1 if sustup_357[_n+1] ==1 & wave==3

	gen sustup2_357=.
	by pid: replace sustup2_357 = 1 if class_ch==2 & egp4==4 & class_ch[_n-1]!=. & class_ch!=. & wavegap==2 & wavegap[_n-1]==2 & wave==7
	by pid: replace sustup2_357 = 1 if sustup2_357[_n+1] ==1 & wave==5
	by pid: replace sustup2_357 = 1 if sustup2_357[_n+1] ==1 & wave==3

	gen sustnoch_357=.
	by pid: replace sustnoch_357 = 1 if class_ch==0 & egp4==2 & class_ch[_n-1]!=. & class_ch!=. & wavegap==2 & wavegap[_n-1]==2 & wave==7
	by pid: replace sustnoch_357 = 1 if sustnoch_357[_n+1] ==1 & wave==5
	by pid: replace sustnoch_357 = 1 if sustnoch_357[_n+1] ==1 & wave==3

		
	* follow individuals: waves 5-7-10, change between 7 and 10
	gen sustup_5710=.
	by pid: replace sustup_5710 = 1 if class_ch==1 & egp4==3 & class_ch[_n-1]!=. & class_ch!=. & wavegap==3 & wavegap[_n-1]==2 & wave==10
	by pid: replace sustup_5710 = 1 if sustup_5710[_n+1] ==1 & wave==7
	by pid: replace sustup_5710 = 1 if sustup_5710[_n+1] ==1 & wave==5

	gen sustup2_5710=.
	by pid: replace sustup2_5710 = 1 if class_ch==2 & egp4==4 & class_ch[_n-1]!=. & class_ch!=. & wavegap==3 & wavegap[_n-1]==2 & wave==10
	by pid: replace sustup2_5710 = 1 if sustup2_5710[_n+1] ==1 & wave==7
	by pid: replace sustup2_5710 = 1 if sustup2_5710[_n+1] ==1 & wave==5

	gen sustnoch_5710=.
	by pid: replace sustnoch_5710 = 1 if class_ch==0 & egp4==2 & class_ch[_n-1]!=. & class_ch!=. & wavegap==3 & wavegap[_n-1]==2 & wave==10
	by pid: replace sustnoch_5710 = 1 if sustnoch_5710[_n+1] ==1 & wave==7
	by pid: replace sustnoch_5710 = 1 if sustnoch_5710[_n+1] ==1 & wave==5
		
	
	* follow individuals: waves 7-10-14, change between 7 and 10
	gen sustup_71014=.
	by pid: replace sustup_71014 = 1 if class_ch==1 & egp4==3 & class_ch[_n-1]!=. & class_ch!=. & wavegap==4 & wavegap[_n-1]==3 & wave==14
	by pid: replace sustup_71014 = 1 if sustup_71014[_n+1] ==1 & wave==10
	by pid: replace sustup_71014 = 1 if sustup_71014[_n+1] ==1 & wave==7

	gen sustup2_71014=.
	by pid: replace sustup2_71014 = 1 if class_ch==2 & egp4==4 & class_ch[_n-1]!=. & class_ch!=. & wavegap==4 & wavegap[_n-1]==3 & wave==14
	by pid: replace sustup2_71014 = 1 if sustup2_71014[_n+1] ==1 & wave==10
	by pid: replace sustup2_71014 = 1 if sustup2_71014[_n+1] ==1 & wave==7
	
	gen sustnoch_71014=.
	by pid: replace sustnoch_71014 = 1 if class_ch==0 & egp4==2 & class_ch[_n-1]!=. & class_ch!=. & wavegap==4 & wavegap[_n-1]==3 & wave==14
	by pid: replace sustnoch_71014 = 1 if sustnoch_71014[_n+1] ==1 & wave==10
	by pid: replace sustnoch_71014 = 1 if sustnoch_71014[_n+1] ==1 & wave==7
	
	
	* follow individuals: waves 10-14-17, change between 14 and 17
	gen sustup_101417=.
	by pid: replace sustup_101417 = 1 if class_ch==1 & egp4==3 & class_ch[_n-1]!=. & class_ch!=. & wavegap==3 & wavegap[_n-1]==4 & wave==17
	by pid: replace sustup_101417 = 1 if sustup_101417[_n+1] ==1 & wave==14
	by pid: replace sustup_101417 = 1 if sustup_101417[_n+1] ==1 & wave==10

	gen sustup2_101417=.
	by pid: replace sustup2_101417 = 1 if class_ch==2 & egp4==4 & class_ch[_n-1]!=. & class_ch!=. & wavegap==3 & wavegap[_n-1]==4 & wave==17
	by pid: replace sustup2_101417 = 1 if sustup2_101417[_n+1] ==1 & wave==14
	by pid: replace sustup2_101417 = 1 if sustup2_101417[_n+1] ==1 & wave==10
	
	gen sustnoch_101417=.
	by pid: replace sustnoch_101417 = 1 if class_ch==0 & egp4==2 & class_ch[_n-1]!=. & class_ch!=. & wavegap==3 & wavegap[_n-1]==4 & wave==17
	by pid: replace sustnoch_101417 = 1 if sustnoch_101417[_n+1] ==1 & wave==14
	by pid: replace sustnoch_101417 = 1 if sustnoch_101417[_n+1] ==1 & wave==10
	

	* for the appendix fig on changes in class and changes (done in R)
	by pid: gen lastclass = egp4[_n-1]
	
	gen ideol_ch=.
	by pid: replace ideol_ch = eclr - eclr[_n-1] 
	
	
	* export to R
	cd ""
	save "paralleltrends_forR.dta", replace
	
	* robustness check removing non-parallel individuals (appendix)
	drop if sustup_71014==1
	drop if sustup2_101417==1
	xtreg eclr worker lowservice highservice i.wave, fe cl(pid) 
	
	restore
	
***********************
* Sustained Changes Analysis (main paper fig 1 and appendix)
cd ""
gen tclass_ch=.
	by pid: replace tclass_ch = egp4 - egp4[_n-1] 
	
	gen tclass_ch2=.
	by pid: replace tclass_ch2 = egp4[_n-1] - egp4[_n-2]
	
	gen tclass_ch3=.
	by pid: replace tclass_ch3 = egp4[_n-2]  - egp4[_n-3]
	
	gen tclass_ch4=.
	by pid: replace tclass_ch4 = egp4[_n-3]  - egp4[_n-4]
	
	gen tclass_ch5=.
	by pid: replace tclass_ch5 = egp4[_n-4]  - egp4[_n-5]
	
* eclr	
 preserve
 keep if wave==1|wave==3|wave==5|wave==7|wave==10|wave==14|wave==17 /*restrict to waves where we observe ideology*/
	keep if eclr!=.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	gen wavegap =.
	by pid: replace wavegap = wave - wave[_n-1]

	gen ideol_ch=.
	by pid: replace ideol_ch = eclr - eclr[_n-1] 
	
	gen class_ch=.
	replace class_ch = egp4 - egp4[_n-1]
	
	by pid: gen lastclass = egp4[_n-1]
	
	* export to R
	save "final_sustained_econ_forR.dta", replace
restore
	
* Gender
preserve
	keep if wave==1|wave==3|wave==5|wave==7|wave==11|wave==13|wave==15|wave==17	
	keep if wifehome!=.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	gen wavegap =.
	by pid: replace wavegap = wave - wave[_n-1]

	gen ideol_ch=.
	by pid: replace ideol_ch = wifehome - wifehome[_n-1] 
	
	gen class_ch=.
	replace class_ch = egp4 - egp4[_n-1]
	
	by pid: gen lastclass = egp4[_n-1]
	
	summ ideol_ch if class_ch==0
	
	* export to R
	save "final_sustained_gender_forR.dta", replace
restore	


* Homosexuality
preserve
	keep if wave==8|wave==10|wave==12|wave==14
	drop if homowrong==.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	gen wavegap =.
	by pid: replace wavegap = wave - wave[_n-1]

	gen ideol_ch=.
	by pid: replace ideol_ch = homowrong - homowrong[_n-1] 
	
	gen class_ch=.
	replace class_ch = egp4 - egp4[_n-1]
	
	by pid: gen lastclass = egp4[_n-1]
	
	summ ideol_ch if class_ch==0 & lastclass==2
	
	* export to R
	save "final_sustained_homosex_forR.dta", replace
restore	
	
	

** SECOND TYPE OF ANALYSIS: JUST LOOKING ONE PERIOD AFTER 	

preserve
keep if wave==1|wave==3|wave==5|wave==7|wave==10|wave==14|wave==17 /*restrict to waves where we observe ideology*/
	keep if eclr!=.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	gen class_ch=.
	by pid: replace class_ch = egp4 - egp4[_n-1] 
	
	gen class_ch2=.
	by pid: replace class_ch2 = egp4[_n-1] - egp4[_n-2] 

	gen ideol_ch=.
	by pid: replace ideol_ch = eclr - eclr[_n-2] 
	
	gen ideol_ch_prev=.
	by pid: replace ideol_ch_prev = eclr[_n-1] - eclr[_n-2] 
	
	by pid: gen lastclass = egp4[_n-1]

	gen sustup=.
	by pid: replace sustup = 1 if class_ch2==1 & class_ch>=0 & lastclass==3 

	summ ideol_ch if sustup==1
	summ ideol_ch_prev if sustup==1
	
	gen sustup2=.
	by pid: replace sustup2 = 1 if class_ch2==2 & class_ch>=0 & lastclass==4 

	summ ideol_ch if sustup2==1
	summ ideol_ch_prev if sustup2==1
	
	gen sustnoch=.
	by pid: replace sustnoch = 1 if class_ch2==0 & class_ch==0 & egp4==2
	
	summ ideol_ch if sustnoch==1
	summ ideol_ch_prev if sustnoch==1
	
	* export to R
	save "final_sustained_econ_forR2.dta", replace
restore	

preserve
keep if wave==1|wave==3|wave==5|wave==7|wave==11|wave==13|wave==15|wave==17	
	keep if wifehome!=.
	
	drop nobs
	bys pid: gen nobs=[_N] 
	drop if nobs==1
	
	gen class_ch=.
	by pid: replace class_ch = egp4 - egp4[_n-1] 
	
	gen class_ch2=.
	by pid: replace class_ch2 = egp4[_n-1] - egp4[_n-2] 
	
	gen ideol_ch=.
	by pid: replace ideol_ch = wifehome - wifehome[_n-2] 
	
	gen ideol_ch_prev=.
	by pid: replace ideol_ch_prev = wifehome[_n-1] - wifehome[_n-2] 

	by pid: gen lastclass = egp4[_n-1]

		gen sustup=.
	by pid: replace sustup = 1 if class_ch2==1 & class_ch>=0 & lastclass==3 

	summ ideol_ch if sustup==1
	summ ideol_ch_prev if sustup==1
	
	gen sustup2=.
	by pid: replace sustup2 = 1 if class_ch2==2 & class_ch>=0 & lastclass==4 

	summ ideol_ch if sustup2==1
	summ ideol_ch_prev if sustup2==1
	
	gen sustnoch=.
	by pid: replace sustnoch = 1 if class_ch2==0 & class_ch==0 & egp4==2
	
	summ ideol_ch if sustnoch==1
	summ ideol_ch_prev if sustnoch==1
	
	* export to R
	save "final_sustained_gender_forR2.dta", replace
restore
	

	
*********************************************************************************************************************

** APPENDIX ANALYSIS


* Attrition
* ------------------------------------------- 
	preserve
	
	keep if wave==1|wave==3|wave==5|wave==7|wave==10|wave==14|wave==17 /*restrict to waves where we observe ideology*/
	 
	* number of individuals pre attrition/item non-response 
	drop if eclr==.  
	codebook pid
	
	* identify people in for one period
	drop nobs
	bys pid: gen nobs=[_N] 
	
	/* * number of individuals post attrition 
	drop if nobs==1
	codebook pid */
	
	* identify start wave and first wave
	by pid: egen start=min(wave)
	by pid: egen end=max(wave)
	gen firstwave=.
	by pid: replace firstwave = 1 if wave==start
	
	
	* analysis of people in for one wave only vs. sample
	gen inthesample =.
	replace inthesample = 1 if nobs !=1 & firstwave==1
	replace inthesample = 0 if nobs ==1
	
	tab inthesample
	
	gen worker1 = .
	replace worker1 = 1 if worker==1 & inthesample==1
	replace worker1 = 0 if worker==0 & inthesample==1
	
	gen worker2 = .
	replace worker2 = 1 if worker==1 & inthesample==0
	replace worker2 = 0 if worker==0 & inthesample==0
	
	gen lowservice1 = .
	replace lowservice1 = 1 if lowservice==1 & inthesample==1
	replace lowservice1 = 0 if lowservice==0 & inthesample==1
	
	gen lowservice2 = .
	replace lowservice2 = 1 if lowservice==1 & inthesample==0
	replace lowservice2 = 0 if lowservice==0 & inthesample==0
	
	gen highservice1 = .
	replace highservice1 = 1 if highservice==1 & inthesample==1
	replace highservice1 = 0 if highservice==0 & inthesample==1
	
	gen highservice2 = .
	replace highservice2 = 1 if highservice==1 & inthesample==0
	replace highservice2 = 0 if highservice==0 & inthesample==0
	
	gen female1 = .
	replace female1 = 1 if sex==2 & inthesample==1
	replace female1 = 0 if sex==1 & inthesample==1
	
	gen female2 = .
	replace female2 = 1 if sex==2 & inthesample==0
	replace female2 = 0 if sex==1 & inthesample==0
	
	gen edulower1 = .
	replace edulower1 = 1 if edu3==1 & inthesample==1
	replace edulower1 = 0 if edu3>1 & edu3<4 & inthesample==1
	
	gen edulower2 = .
	replace edulower2 = 1 if edu3==1 & inthesample==0
	replace edulower2 = 0 if edu3>1 & edu3<4 & inthesample==0
	
	gen eduhigher1 = .
	replace eduhigher1 = 1 if edu3==3 & inthesample==1
	replace eduhigher1 = 0 if edu3<3 & inthesample==1
	
	gen eduhigher2 = .
	replace eduhigher2 = 1 if edu3==3 & inthesample==0
	replace eduhigher2 = 0 if edu3<3 & inthesample==0
	
	prtest worker1==worker2
	prtest lowservice1==lowservice2
	prtest highservice1==highservice2
	prtest female1 == female2
	prtest edulower1 == edulower2
	prtest eduhigher1 == eduhigher2
	ttest age, by(inthesample)
	ttest fiyr1000, by(inthesample)
	ttest eclr, by(inthesample)
	
	
	* analysis of full versus unfull
	
	tab nobs
	tab start
	
	drop if nobs==1
	
	* wave 1 sample
	gen full =.
	replace full = 1 if start==1 & end==17 & firstwave==1
	replace full =0 if start==1 & end<17 & firstwave==1
	
	tab full

	drop worker1 worker2 lowservice1 lowservice2 highservice1 highservice2 female1 female2 edulower1 edulower2 eduhigher1 eduhigher2
	
	gen worker1 = .
	replace worker1 = 1 if worker==1 & full==1
	replace worker1 = 0 if worker==0 & full==1
	
	gen worker2 = .
	replace worker2 = 1 if worker==1 & full==0
	replace worker2 = 0 if worker==0 & full==0
	
	gen lowservice1 = .
	replace lowservice1 = 1 if lowservice==1 & full==1
	replace lowservice1 = 0 if lowservice==0 & full==1
	
	gen lowservice2 = .
	replace lowservice2 = 1 if lowservice==1 & full==0
	replace lowservice2 = 0 if lowservice==0 & full==0
	
	gen highservice1 = .
	replace highservice1 = 1 if highservice==1 & full==1
	replace highservice1 = 0 if highservice==0 & full==1
	
	gen highservice2 = .
	replace highservice2 = 1 if highservice==1 & full==0
	replace highservice2 = 0 if highservice==0 & full==0
	
	gen female1 = .
	replace female1 = 1 if sex==2 & full==1
	replace female1 = 0 if sex==1 & full==1
	
	gen female2 = .
	replace female2 = 1 if sex==2 & full==0
	replace female2 = 0 if sex==1 & full==0
	
	gen edulower1 = .
	replace edulower1 = 1 if edu3==1 & full==1
	replace edulower1 = 0 if edu3>1 & edu3<4 & full==1
	
	gen edulower2 = .
	replace edulower2 = 1 if edu3==1 & full==0
	replace edulower2 = 0 if edu3>1 & edu3<4 & full==0
	
	gen eduhigher1 = .
	replace eduhigher1 = 1 if edu3==3 & full==1
	replace eduhigher1 = 0 if edu3<3 & full==1
	
	gen eduhigher2 = .
	replace eduhigher2 = 1 if edu3==3 & full==0
	replace eduhigher2 = 0 if edu3<3 & full==0
	
	prtest worker1==worker2
	prtest lowservice1==lowservice2
	prtest highservice1==highservice2
	prtest female1 == female2
	prtest edulower1 == edulower2
	prtest eduhigher1 == eduhigher2
	ttest age, by(full)
	ttest fiyr1000, by(full)
	ttest eclr, by(full)
	
	
	* wave 10 refreshment sample
	drop full worker1 worker2 lowservice1 lowservice2 highservice1 highservice2 female1 female2 edulower1 edulower2 eduhigher1 eduhigher2
	gen full =.
	replace full = 1 if start==10 & end==17 & firstwave==1
	replace full =0 if start==10 & end<17 & firstwave==1
	
	tab full

	gen worker1 = .
	replace worker1 = 1 if worker==1 & full==1
	replace worker1 = 0 if worker==0 & full==1
	
	gen worker2 = .
	replace worker2 = 1 if worker==1 & full==0
	replace worker2 = 0 if worker==0 & full==0
	
	gen lowservice1 = .
	replace lowservice1 = 1 if lowservice==1 & full==1
	replace lowservice1 = 0 if lowservice==0 & full==1
	
	gen lowservice2 = .
	replace lowservice2 = 1 if lowservice==1 & full==0
	replace lowservice2 = 0 if lowservice==0 & full==0
	
	gen highservice1 = .
	replace highservice1 = 1 if highservice==1 & full==1
	replace highservice1 = 0 if highservice==0 & full==1
	
	gen highservice2 = .
	replace highservice2 = 1 if highservice==1 & full==0
	replace highservice2 = 0 if highservice==0 & full==0
	
	gen female1 = .
	replace female1 = 1 if sex==2 & full==1
	replace female1 = 0 if sex==1 & full==1
	
	gen female2 = .
	replace female2 = 1 if sex==2 & full==0
	replace female2 = 0 if sex==1 & full==0
	
	gen edulower1 = .
	replace edulower1 = 1 if edu3==1 & full==1
	replace edulower1 = 0 if edu3>1 & edu3<4 & full==1
	
	gen edulower2 = .
	replace edulower2 = 1 if edu3==1 & full==0
	replace edulower2 = 0 if edu3>1 & edu3<4 & full==0
	
	gen eduhigher1 = .
	replace eduhigher1 = 1 if edu3==3 & full==1
	replace eduhigher1 = 0 if edu3<3 & full==1
	
	gen eduhigher2 = .
	replace eduhigher2 = 1 if edu3==3 & full==0
	replace eduhigher2 = 0 if edu3<3 & full==0
	

	prtest worker1==worker2
	prtest lowservice1==lowservice2
	prtest highservice1==highservice2
	prtest female1 == female2
	prtest edulower1 == edulower2
	prtest eduhigher1 == eduhigher2
	ttest age, by(full)
	ttest fiyr1000, by(full)
	ttest eclr, by(full)

	restore 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 